class TreeNode {
    constructor(val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
}

const COMPARE_MAP = {
    LESS_THAN: -1,
    MORE_THAN: 1,
    EQUAL: 0
}

function defaultCompare(m, n) {
    return m - n > 0 ?  COMPARE_MAP.MORE_THAN : m - n == 0 ? COMPARE_MAP.EQUAL : COMPARE_MAP.LESS_THAN;
}

class BST {
    constructor(compare = defaultCompare)  {
        this.root = null;
        this.compare = compare;
    }
    insert(val) {
        if (this.root == null) {
            this.root = new TreeNode(val);
        }  else {
            this._insertNode(this.root, val);
        }
    }
    _insertNode(node, val) {
        if (this.compare(node.val, val) === COMPARE_MAP.LESS_THAN) {
            if (node.left == null) {
                node.left = new TreeNode(val);
            } else {
                this._insertNode(node.left, val);
            }
        } else {
            if (node.right == null) {
                node.right = new TreeNode(val)
            }  else {
                this._insertNode(node.right, val);
            }
        }
    }
    inOrderTraverse(callback) {
        this._inOrderTraverseNode(this.root, callback);
    }
    _inOrderTraverseNode(node, callback) {
        if (node == null) return;
        callback(node.val);
        this._inOrderTraverseNode(node.left, callback);
        this._inOrderTraverseNode(node.right, callback);
    }
    preOrderTraverse() {

    }
    postOrderTraverse() {

    }
}

export default BST;